package com.iretailer.report.web.system;

import com.iretailer.report.base.BaseAction;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.dto.ReMenus;
import com.iretailer.report.model.Menus;
import com.iretailer.report.model.Role;
import com.iretailer.report.model.User;
import com.iretailer.report.util.RMap;
import com.iretailer.report.util.SecurityUserUtil;
import com.iretailer.report.util.StringUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Auther: xjw
 * @Date: 2019/1/25 17:11
 * @Description:
 */
@RestController
public class MenuController extends BaseAction {


    @RequestMapping("/system/userMenuTree.action")
    public Map<String, Object> userMenu(HttpServletRequest request) {
         User user = SecurityUserUtil.currentUser(request);
        String roleId = user.getRoleId();
        //查询出菜单
        //管理员
        List<Menus> menus = new ArrayList<>();
        if (StringUtil.notEmpty(roleId) && roleId.trim().equals("-")) {
            menus = menusService.selectMenusByVisible(null);
        } else {
            Role role = roleService.getById(roleId);
            menus = menusService.selectMenusByRoleId(role.getId());
        }
        Menus first = Menus.FirstMenus;
        List<Menus> menusTree = menusService.convertTree(menus, "-");
        first.setChildren(menusTree);

        return RMap.ok(new Menus[]{first});
    }

    @RequestMapping("/system/gridMenu.action")
    public Map<String, Object> gridMenu(@RequestBody ReMenus reMenus) {
        QueryOption queryOption = modelMapper.map(reMenus, QueryOption.class);
        PageResult<Menus> find = menusService.selectByParentIdPage(reMenus.getParentId(), queryOption);

        return RMap.ok(find);
    }

    @RequestMapping("/system/deleteMenu.action")
    public Map<String, Object> deleteMenu(@RequestBody List<String> ids) {
        boolean result = menusService.deleteByIds(ids);
        if (result) {
            return RMap.ok();
        } else {
            return RMap.error(RMap.Code.CONNECTED, "have_children_menu");
        }
    }

    @RequestMapping("/system/selectMenutById.action")
    public Map<String, Object> selectMenutById(@RequestBody ReMenus reMenus) {
        if (reMenus.getId().equals("-")) {
            return RMap.ok(Menus.FirstMenus);
        } else {
            Menus find = menusService.getById(reMenus.getId());
            return RMap.ok(find);
        }
    }

    @RequestMapping("/system/saveOrUpdateMenu.action")
    public Map<String, Object> saveOrUpdateMenu(@RequestBody ReMenus reMenus) {
        Menus menus = modelMapper.map(reMenus, Menus.class);
        boolean result = menusService.saveOrUpdate(menus);
        return RMap.resultBoolean(result);
    }
}
